package game.screen;

import game.ability.AbilityCloudFire;
import game.ability.AbilityManager;
import game.ability.AbsAbility;
import game.ability.Lightning;
import game.ability.MovingLeft;
import game.ability.Shot;
import game.ability.SnowDown;
import game.entity.AbsSprite;
import game.entity.Army;
import game.entity.BufferSpriteManager;
import game.entity.Monster;
import game.entity.SpriteManager;
import game.helper.ArmyBoundary;
import game.helper.Input;
import game.helper.Level;
import game.helper.ShakeBackground;
import game.logic.GameControl;
import game.particles.Particle_1;
import game.shared.Constants;
import game.shared.Resources;
import game.windows.Bunker;
import game.windows.Button;
import game.windows.Form;
import game.windows.ToggleButton;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class ScreenPlaying extends AbsScreen {
	SpriteManager armyManager;
	SpriteManager weaponManager;
	SpriteManager monsterManager;
	Constants constants;
	BufferSpriteManager bufferSpriteManager;
	SpriteBatch sb;

	float timer;
	private BitmapFont font;
	ShakeBackground shakeBackground;
	TextureRegion boder;
	TextureRegion hpTextureRegion;
	TextureRegion manaTextureRegion;
	TextureRegion processTextureRegion;

	ShapeRenderer shapeRenderer;
	Camera camera;
	Rectangle viewport;

	boolean bPause, bBunker;

	AbilityManager abilityManager;

	Form myRealizes;
	Form popupMenu;
	Bunker bunker;
	Random random;
	
	Music music;
	Lightning lightning2;

	Level level;
	public ScreenPlaying(GameControl _gameControl) {
		super(_gameControl);
        
		id = Constants.SCREEN_PLAYING;
		random = new Random();
		armyManager = new SpriteManager();
		weaponManager = new SpriteManager();
		monsterManager = new SpriteManager();
		constants = new Constants();
		abilityManager = new AbilityManager();
		BufferSpriteManager.Init(abilityManager, gameControl, armyManager, monsterManager, weaponManager);
		sb = new SpriteBatch();
		Resources.LoadResource("res/resources.txt");
		timer = 0;
		font = new BitmapFont();

		shapeRenderer = new ShapeRenderer();
		camera = new OrthographicCamera(Constants.SCREEN_WIDTH,
				Constants.SCREEN_HEIGHT);
		camera.update();
		shapeRenderer.setProjectionMatrix(camera.combined);
		viewport = new Rectangle(0, 0, 100, 100);
		initRealizes();
		LoadResource(); // Phai luon de duoi cung.
		
		popupMenu = new Form();
		popupMenu.setbDrawWatermark(true);
		createPopupMenu();
		
		bunker = new Bunker();
		bBunker = false;
		bunker.setbDrawWatermark(true);
		music = Gdx.audio.newMusic(Gdx.files.internal("res/sound/bg.mp3"));
		level = new Level();
	}
	
	
	@Override
	public void show() {
		music.play();
		super.show();
	}

	@Override
	public void update() {
		if (bPause)
		{
			music.pause();
			if(bBunker)
				bunker.update();
			else
				popupMenu.update();
			checkMessage();
			return;
		}

		myRealizes.update();
		checkMessage();

		// abilityManager.
		armyManager.update();
		weaponManager.update();
		monsterManager.update();

		abilityManager.update();

		timer += Gdx.graphics.getDeltaTime();
		if (timer > 1.5) {
			timer = 0;
			addResource();
		}
//		level.update();
		shakeBackground.update();
	}

	int a = 0, b = 0;

	private void checkMessage() {
		switch (Constants.get_RETURN_CODE()) {
		case Constants.FUNCTION_CODE_SNOW:
			if (Constants.MANA > 10)
				if (Constants.get_RETURN_VALUE() == 1) {
					a++;
					SnowDown snow = new SnowDown(0.4f, 5); // snow in 5seconds
					snow.setTarget(monsterManager);
					snow.action();
					abilityManager.add(snow);
					Constants.MANA -= 10;
				}
			break;
		case Constants.FUNCTION_CODE_METEORITE:
			if (Constants.get_RETURN_VALUE() == 1)
				b++;
			if (Constants.get_RETURN_VALUE() == 0)
				b--;
			break;
		case Constants.FUNCTION_CODE_CLOUDFIRE:
			if (Constants.MANA > 10)
				if (Constants.get_RETURN_VALUE() == 1) {
					AbilityCloudFire cloudFire = new AbilityCloudFire();
					cloudFire.setTarget(monsterManager);
					cloudFire.action();
					abilityManager.add(cloudFire);
					Constants.MANA -= 10;
				}
			break;
		case Constants.FUNCTION_CODE_NEWGAME:
			Constants.set_RETURN_CODE(Constants.FUNCTION_CODE_NEWGAME);
			gameControl.setScreen(Constants.SCREEN_PLAYING);
			restart();
			resumeGame();
			break;
		case Constants.FUNCTION_CODE_MENU:
			pauseGame();
			bBunker = false;
			break;
		case Constants.FUNCTION_CODE_RESUME:
			resumeGame();
			break;
		case Constants.FUNCTION_CODE_SHOP:
			bBunker = true;
			break;
		case Constants.FUNCTION_CODE_CREDIT:
			break;
		case Constants.FUNCTION_CODE_EXIT:
			pauseGame();
			gameControl.setScreen(Constants.SCREEN_MENU);
			break;
		default:
			break;
		}

		Constants.set_RETURN_CODE(Constants.FUNCTION_CODE_NONE); // !importance
	}

	private void pauseGame() {
		armyManager.pause();
		weaponManager.pause();
		monsterManager.pause();
		bPause = true;
	}

	private void resumeGame() {
		armyManager.resume();
		weaponManager.resume();
		monsterManager.resume();
		bPause = false;
		bBunker = false;
		music.play();
	}

	@Override
	public void render(float arg0) {
		
		shakeBackground.render(shapeRenderer);
		
		sb.begin();
		armyManager.render(sb);
		weaponManager.render(sb);
		monsterManager.render(sb);

		font.setColor(Color.WHITE);
		
		font.draw(sb, String.valueOf(timer), Constants.SCREEN_WIDTH - 100,
				Constants.SCREEN_HEIGHT - 100);
		font.draw(sb, String.valueOf(a), Constants.SCREEN_WIDTH - 100,
				Constants.SCREEN_HEIGHT - 120);
		font.draw(sb, String.valueOf(b), Constants.SCREEN_WIDTH - 70,
				Constants.SCREEN_HEIGHT - 120);
		
		sb.draw(boder, 0, Constants.SCREEN_HEIGHT - boder.getRegionHeight());
		myRealizes.render(sb);
		font.draw(sb, String.valueOf(Gdx.graphics.getFramesPerSecond()) + " frames/s.", Constants.SCREEN_WIDTH - 100, Constants.SCREEN_HEIGHT - 130);

		AbsSprite xx = monsterManager.getByIndex(monsterManager.count()-1);
		if(xx != null)
			font.draw(sb, String.valueOf(xx.getSlower() * xx.getSpeed()), 70, 250);
		font.draw(sb, String.valueOf(Constants.xx), 100, 250);
		
		if(!bPause){
			abilityManager.render(sb);
			sb.draw(hpTextureRegion,136,286,78*Constants.HP/Constants.HP_MAX,12);
			sb.draw(manaTextureRegion, 136,274,78*Constants.MANA/Constants.MANA_MAX,12);
			sb.draw(processTextureRegion, 50,301,300*Constants.MONSTER_NUMBER/Constants.MONSTER_NUMBER_MAX,11);
			font.setColor(Color.RED);
			font.draw(sb, String.valueOf(Level.level), 70, 302);
			font.draw(sb, String.valueOf(Constants.GOLD), 70, 290);
		}
		if (bPause)
		{
			if(bBunker)
				bunker.render(sb);
			else{
				sb.draw(hpTextureRegion,136,286,78*Constants.HP/Constants.HP_MAX,12);
				sb.draw(manaTextureRegion, 136,274,78*Constants.MANA/Constants.MANA_MAX,12);
				sb.draw(processTextureRegion, 50,301,300*monsterManager.count()/100,11);
				font.setColor(Color.RED);
				font.draw(sb, String.valueOf(Level.level), 70, 302);
				font.draw(sb, String.valueOf(Constants.GOLD), 70, 290);
				popupMenu.render(sb);
			}
		}
		sb.end();
		if (!bPause){
			armyManager.renderShaper();
			shapeRenderer.begin(ShapeType.Line);
			shapeRenderer.line(ArmyBoundary.getA().x-Constants.SCREEN_WIDTH/2, ArmyBoundary.getA().y-Constants.SCREEN_HEIGHT/2, ArmyBoundary.getB().x-Constants.SCREEN_WIDTH/2, ArmyBoundary.getB().y-Constants.SCREEN_HEIGHT/2);
			shapeRenderer.end();
		}
	}
	
	@Override
	public void resume() {
//		switch (Constants.get_RETURN_CODE()) 
//		{
//		case Constants.FUNCTION_CODE_NEWGAME:
//			restart();
//			break;
//
//		default:
//			break;
//		}
	}

	private void restart() 
	{
		Constants.set_RETURN_VALUE(0); 
		Constants.set_RETURN_CODE(Constants.FUNCTION_CODE_NONE);
		monsterManager.removeAll();
		armyManager.removeAll();
		weaponManager.removeAll();
		timer = 0;
		LoadResource();
	}

	private void LoadResource() {
		Monster monter_2 = new Monster(BufferSpriteManager.getSpriteBufferByID(BufferSpriteManager.BUFFER_SPRITE_MONSTER_4));
		monter_2.setMoving(new MovingLeft(Constants.SPEED_MONSTER));
		monter_2.setPosition(400, 130);
		monsterManager.add(monter_2);

		Army army_castle = new Army(gameControl, monsterManager, "res/img_home/level_1.txt");
		army_castle.setPosition(0, 20);
		army_castle.setHp(300);
		armyManager.add(army_castle);

		Army army = new Army(gameControl,BufferSpriteManager.getSpriteBufferByID(BufferSpriteManager.BUFFER_SPRITE_ARMY));
		army.setPosition(25, 150);
		army.setHp(1000);
		army.setInput(new Input(army));
		Lightning lightning = new Lightning(monsterManager, camera, viewport,
				shapeRenderer);
		army.addAbility(lightning);
		armyManager.add(army);
		shakeBackground = new ShakeBackground(level);
		boder = Resources.getTextureRegion(Resources.BODER);
		hpTextureRegion = Resources.getTextureRegion(Resources.HP_TEXTURE);
		manaTextureRegion = Resources.getTextureRegion(Resources.MANA_TEXTURE);
		processTextureRegion = Resources.getTextureRegion(Resources.PROCESS_TEXTURE);

		lightning2 = new Lightning(monsterManager, camera, viewport,
				shapeRenderer);
	}

	public void addResource() {
		level.addResource(sb, abilityManager, armyManager, monsterManager, weaponManager, random);
	}

	public void createPopupMenu() {
		popupMenu = new Form();
		popupMenu.setbDrawWatermark(true);
		popupMenu.setXY(185, 70);

		Button bt = new Button(Constants.FUNCTION_CODE_RESUME,
				Resources.getTextureRegion(Resources.MENU_RESUME),
				Resources.getTextureRegion(Resources.MENU_RESUME_2));
		bt.setXY(0, 150);
		popupMenu.addControl(bt);
		
		bt = new Button(Constants.FUNCTION_CODE_NEWGAME,
				Resources.getTextureRegion(Resources.MENU_NEWGAME),
				Resources.getTextureRegion(Resources.MENU_NEWGAME_2));
		bt.setXY(0, 120);
		popupMenu.addControl(bt);
		
		bt = new Button(Constants.FUNCTION_CODE_OPTION,
				Resources.getTextureRegion(Resources.MENU_OPTION),
				Resources.getTextureRegion(Resources.MENU_OPTION_2));
		bt.setXY(0, 90);
		popupMenu.addControl(bt);
		bt = new Button(Constants.FUNCTION_CODE_CREDIT,
				Resources.getTextureRegion(Resources.MENU_CREDIT),
				Resources.getTextureRegion(Resources.MENU_CREDIT_2));
		bt.setXY(0, 60);
		popupMenu.addControl(bt);
		
		bt = new Button(Constants.FUNCTION_CODE_SHOP,
				Resources.getTextureRegion(Resources.MENU_SHOP),
				Resources.getTextureRegion(Resources.MENU_SHOP_2));
		bt.setXY(0, 30);
		popupMenu.addControl(bt);
		
		bt = new Button(Constants.FUNCTION_CODE_EXIT,
				Resources.getTextureRegion(Resources.MENU_EXIT),
				Resources.getTextureRegion(Resources.MENU_EXIT_2));
		bt.setXY(0, 0);
		popupMenu.addControl(bt);
	}
	
	private void initRealizes() {
		myRealizes = new Form();
		Button buttonSnow = new Button(Constants.FUNCTION_CODE_SNOW,
				Resources.getTextureRegion(Resources.BUTTON_1), Resources.getTextureRegion(Resources.BUTTON_2));
		Button buttonCloudFire = new Button(Constants.FUNCTION_CODE_CLOUDFIRE,
				Resources.getTextureRegion(8), Resources.getTextureRegion(Resources.BUTTON_2));
		ToggleButton button2 = new ToggleButton(Constants.FUNCTION_CODE_METEORITE,
				Resources.getTextureRegion(7), Resources.getTextureRegion(Resources.BUTTON_2));
		Button buttonMenu = new Button(Constants.FUNCTION_CODE_MENU,
				Resources.getTextureRegion(Resources.MENU_MENU), Resources.getTextureRegion(Resources.MENU_MENU_2));

		buttonSnow.setXY(220, 274);
		button2.setXY(260, 274);
		buttonCloudFire.setXY(300, 274);
		buttonMenu.setXY(390, 274);
		myRealizes.addControl(buttonSnow);
		myRealizes.addControl(buttonCloudFire);
		myRealizes.addControl(button2);
		myRealizes.addControl(buttonMenu);
	}

}
